package com.jiangjiang.eshop.logistics.service;

import com.jiangjiang.eshop.commodity.dto.GoodsDTO;
import com.jiangjiang.eshop.commodity.dto.GoodsSkuDTO;
import com.jiangjiang.eshop.commodity.service.GoodsService;
import com.jiangjiang.eshop.common.help.DateHelper;
import com.jiangjiang.eshop.logistics.calculator.FreightCalculator;
import com.jiangjiang.eshop.logistics.calculator.FreightCalculatorFactory;
import com.jiangjiang.eshop.logistics.dto.FreightTemplateDTO;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;

/**
 * @ClassName LogisticsFacadeServiceImpl
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/16 11:53
 * @Version 1.0
 **/
@Slf4j
@Service
public class LogisticsFacadeServiceImpl implements LogisticsFacadeService {
    @Autowired
    private DateHelper dateHelper;
    @Autowired
    private FreightCalculatorFactory freightCalculatorFactory;
    @Autowired
    private FreightTemplateService freightTemplateService;
    @Autowired
    private GoodsService goodsService;


    @Override
    public Double calculateFreight(OrderInfoDTO order, OrderItemDTO orderItem) {
        try {
            GoodsDTO goodsDTO = goodsService.queryById(orderItem.getGoodsId());
            FreightTemplateDTO freightTemplate = freightTemplateService.queryById(goodsDTO.getFreightTemplateId());
            FreightCalculator freightCalculator = freightCalculatorFactory.get(freightTemplate);
            return freightCalculator.calculate(freightTemplate, order, orderItem);
        } catch (Exception e) {
            log.warn("LogisticsFacadeServiceImpl calculateFreight error",e);
        }
        return 0.0;
    }

    @Override
    public Date getSignedTime(Long orderId, String orderNo) throws Exception {
        return dateHelper.getCurrentDate();
    }
}
